package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.JsonResp;
import com.rongke.model.Admin;
import com.rongke.model.AdminRolePermission;
import com.rongke.model.AdminUserRole;
import com.rongke.redis.CacheUtil;
import com.rongke.service.AdminRolePermissionService;
import com.rongke.service.AdminService;
import com.rongke.service.AdminUserRoleService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @AdminRolePermissionController
 * @Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/adminRolePermission")
@Transactional
@CrossOrigin
public class AdminRolePermissionController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private AdminRolePermissionService adminRolePermissionService;
    @Autowired
    private CacheUtil cacheUtil;
    @Autowired
    private AdminUserRoleService adminUserRoleService;
    @Autowired
    private AdminService adminService;
    /**
     * @添加
     * @param rodeId 角色id
     *        catalogId 目录ids
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.GET)
    public JsonResp addAdminUserRole(Integer rodeId,String catalogId){
        log.debug("添加");
        String catalogIdArr[]=catalogId.split(",");
        List<AdminRolePermission> adminRolePermissions=new ArrayList<>();
        for (int i=0;i<catalogIdArr.length;i++){
            AdminRolePermission arp=new AdminRolePermission();
            arp.setAdminRoleId(rodeId);
            arp.setAdminPermissionId(Integer.valueOf(catalogIdArr[i]));
            adminRolePermissions.add(arp);
        }
        if(!adminRolePermissions.isEmpty()){
            EntityWrapper<AdminRolePermission> ew=new EntityWrapper<>();
            ew.eq("admin_role_id",rodeId);
            adminRolePermissionService.delete(ew);
            adminRolePermissionService.insertBatch(adminRolePermissions);
        }
        EntityWrapper<AdminUserRole> ew=new EntityWrapper<>();
        ew.eq("admin_role_id",rodeId);
        AdminUserRole adminUserRole=adminUserRoleService.selectOne(ew);
        if(adminUserRole!=null){
            Admin admin=adminService.selectById(adminUserRole.getAdminId());
            if(cacheUtil.hasKey("menu"+admin.getLoginName())){
                cacheUtil.delkey("menu"+admin.getLoginName());
            }
        }
        return JsonResp.ok();
    }

    /**
     * @修改
     * @param adminRolePermission
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateAdminRolePermission(@RequestBody AdminRolePermission adminRolePermission){
        log.debug("修改");
        adminRolePermissionService.updateById(adminRolePermission);
        return JsonResp.ok(adminRolePermission);
    }
    /**
     * @根据id查找
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectAdminRolePermission(Long id){
        log.debug("查找");
        AdminRolePermission adminRolePermission = adminRolePermissionService.selectById(id);
        return JsonResp.ok(adminRolePermission);
    }

    /**
     * @根据条件查找
     * @param adminRolePermission
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectAdminRolePermissionByCondition(@RequestBody AdminRolePermission adminRolePermission){
        log.debug("根据条件查找");
        if(adminRolePermission==null)
            return JsonResp.toFail("参数不能为空");
        List<AdminRolePermission> adminRolePermissionList=adminRolePermissionService.selectByCondition(adminRolePermission);
        return JsonResp.ok(adminRolePermissionList);
    }

}
